﻿namespace YunQue.DataBase
{
    /// <summary>
    /// db context 工厂接口
    /// </summary>
    public interface IDbContextFactory
    {
        /// <summary>
        /// 创建db context
        /// </summary>
        /// <returns></returns>
        YunQueDbContext CreateDbContext();
        /// <summary>
        /// 创建db context
        /// </summary>
        /// <param name="tenantCode"></param>
        /// <param name="userId"></param>
        /// <returns></returns>
        YunQueDbContext CreateDbContext(string tenantCode,string userId);

    }
    /// <summary>
    /// db context 工厂
    /// </summary>
    public class YunQueDbContextFactory : IDbContextFactory
    {
        private readonly UserContext _userContext;
        /// <summary>
        /// 无参构造函数
        /// </summary>
        public YunQueDbContextFactory()
        {
        }

        /// <summary>
        /// 构造函数
        /// </summary>
        /// <param name="userContext"></param>
        public YunQueDbContextFactory(UserContext userContext)
        {
            _userContext = userContext;
        }
        /// <summary>
        /// 创建db context
        /// </summary>
        /// <returns></returns>
        public YunQueDbContext CreateDbContext()
        {
            return new YunQueDbContext(_userContext);
        }
        /// <summary>
        /// 创建db context
        /// </summary>
        /// <param name="tenantCode"></param>
        /// <param name="userId"></param>
        /// <returns></returns>
        public YunQueDbContext CreateDbContext(string tenantCode, string userId)
        {
            UserContext userContext = new UserContext
            {
                Id = userId,
                TenantCode = tenantCode
            };
            return new YunQueDbContext(userContext);
        }
    }
}
